package com.by.auth;

import org.apache.shiro.web.filter.authc.UserFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * 没有用到
 */
@SuppressWarnings("all")
@Deprecated
public class ShiroUserFilter extends UserFilter {
    private static final Logger log = LoggerFactory.getLogger(ShiroUserFilter.class);

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;

        if(log.isErrorEnabled()) {
            log.error("account need login for: {}",  ((HttpServletRequest)request).getServletPath());
        }

        if (!"XMLHttpRequest".equalsIgnoreCase(httpServletRequest
                .getHeader("X-Requested-With"))) {// 不是ajax请求
           return super.onAccessDenied(request, response);
        } else {

            // 请求被拦截后直接返回json格式的响应数据
            response.setContentType("application/json;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.println("{\"code\":1,\"success\":false,\"message\":\"账号未登录\"}");
            out.flush();
            out.close();
            return false;
        }
    }
}
